let rs = require('readline-sync')

let words = ['interface', 'class', 'object', 'array', 'filter', 'pop', 'split']
// 0-words.length (左闭右开)
let random_index = Math.floor(Math.random() * words.length)//让0-1（不包括1）之间的数*数组长度，这样选取数组的下标
// console.log(words[random_index]);      //用随机下标

let select_word = words[random_index] //选中的单词
let char_arr = new Array(select_word.length) //-的个数是新数组里选中的单词的长度(char_arr是数组)
// console.log(select_word) //系统给随机抽取单词
char_arr.fill('-')  //char_arr数组用-填充
console.log(char_arr);

let guess_times = 5

while (true) { //永远是true 永远循环，想跳出用break
    if (guess_times === 0) {
        break
    }
    if (!char_arr.includes('-')) { //包不包含-
        break
    }
    let input_char = rs.question('please input one char:')
    //拿到input_char后，进行处理
    let bFind = false
    for (let i = 0; i < select_word.length; i++) { //遍历
        if (select_word[i] === input_char) {
            char_arr[i] = input_char
            bFind = true
        }
    }
    if (bFind === false) {
        guess_times--
    }
    console.log(char_arr);
    console.log(`you have ${guess_times} left`);
}
//
if (guess_times === 0) {
    console.log('you lose');
}
else {
    console.log('you win');
}